package com.msb.crm.controller;

import com.msb.crm.base.BaseController;
import com.msb.crm.base.ResultInfo;
import com.msb.crm.model.TreeModel;
import com.msb.crm.service.ModuleService;
import com.msb.crm.vo.Module;
import com.msb.crm.vo.Role;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@RequestMapping("module")
@Controller
public class ModuleController extends BaseController {


    @Autowired
    private ModuleService moduleService;

    /**
     * 查询权限
     * @param roleId
     * @return
     */
    @RequestMapping("queryAllModules")
    @ResponseBody
    public List<TreeModel> queryAllModules(Integer roleId){
        return moduleService.queryAllModules(roleId);
    }

    /**
     * 进入资源管理界面
     * @return
     */
    @RequestMapping("index")
    public String index(){
        return "module/module";
    }

    /**
     * 查询所有资源
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String ,Object> moduleList(){
        return moduleService.moduleList();
    }

    /**
     * 添加菜单
     * @param module
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public ResultInfo saveModule(Module module){
        moduleService.saveModule(module);
        return success("菜单添加成功!");
    }


    /**
     * 打开添加资源窗口
     * @param grade
     * @param parentId
     * @param request
     * @return
     */
    @RequestMapping("addModulePage")
    public String addModulePage(Integer grade,Integer parentId,HttpServletRequest request){
        request.setAttribute("grade",grade);
        request.setAttribute("parentId",parentId);
        return "module/add";
    }

    /**
     * 打开编辑页面
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("updateModulePage")
    public String updateModulePage(Integer id,HttpServletRequest request){
        request.setAttribute("module",moduleService.selectByPrimaryKey(id));
        return "module/update";
    }

    @RequestMapping("update")
    @ResponseBody
    public ResultInfo updateModule(Module module){
        moduleService.updateModule(module);
        return success("菜单更新成功!");
    }

    /**
     * 资源删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo deleteModule(Integer id){
        moduleService.deleteModuleById(id);
        return success("菜单删除成功!");
    }




}
